<?php
/**
 * 获取邀请码接口
 * 验证用户身份token
 * @author Edwin
 */

namespace V1\Controller;

use Common\Controller\ApiController;

class InvitationController extends ApiController
{
    public function rest()
    {
        switch ($this->_method) {
            case 'get': // post请求处理代码
                switch (I('get.type')) {
                    case 'get_list':
                        $this->_getList();
                        break;
                    default:
                        $this->_getCode();
                        break;
                }
                break;
        }
    }

    /**
     * 获取邀请码
     */
    private function _getCode()
    {
        $u_id = $this->userId;
        $data = D('MemberProfile')->get_member_code($u_id);
        if (!empty($data['apply_code'])) {
            //生成邀请码
            $code_request = D('MemberProfile')->apply_code_rand($u_id, 7);
            $this->myApiPrint(C('STATUS')['200'], 200, $code_request);
        } else {
            $this->myApiPrint(C('STATUS')['200'], 200, $data['apply_code']);
        }
    }

    /**
     * 获取邀请列表
     */
    private function _getList()
    {
        $code = I('get.code');
        $data = M('member_code')
            ->field('b.userid,b.mobile')
            ->alias('a')
            ->join('LEFT JOIN destoon_ucenter_member AS b ON b.userid = a.uid')
            ->where(array('a.apply_code' => $code))
            ->order('a.id desc')
            ->select();
        if(!$data){
            $this->myApiPrint(C('STATUS')['230'], 201, []);
        }else{
            foreach ($data as $k => $v) {
                $data[$k]['member_info'] = D('MemberProfile')->get_user_details_ask_list($v['userid'], 'get_code');
            }
            $this->myApiPrint(C('STATUS')['200'], 200, $data);
        }
    }
}
